home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / xdr_float.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  7KB  |  276 lines

  1. /*
  2.  * $Id: xdr_float.c,v 1.2 1993/11/14 16:45:50 jraja Exp $
  3.  *
  4.  * $Log: xdr_float.c,v $
  5.  * Revision 1.2  1993/11/14  16:45:50  jraja
  6.  * Fixed include. Added _M68000 to the processor type list.
  7.  *
  8.  */
  9. /* @(#)xdr_float.c    2.1 88/07/29 4.0 RPCSRC */
  10. /*
  11.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12.  * unrestricted use provided that this legend is included on all tape
  13.  * media and as a part of the software program in whole or part.  Users
  14.  * may copy or modify Sun RPC without charge, but are not authorized
  15.  * to license or distribute it to anyone else except as part of a product or
  16.  * program developed by the user.
  17.  * 
  18.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21.  * 
  22.  * Sun RPC is provided with no support and without any obligation on the
  23.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  24.  * modification or enhancement.
  25.  * 
  26.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28.  * OR ANY PART THEREOF.
  29.  * 
  30.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31.  * or profits or other special, indirect and consequential damages, even if
  32.  * Sun has been advised of the possibility of such damages.
  33.  * 
  34.  * Sun Microsystems, Inc.
  35.  * 2550 Garcia Avenue
  36.  * Mountain View, California  94043
  37.  */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  40. #endif
  41.  
  42. /*
  43.  * xdr_float.c, Generic XDR routines impelmentation.
  44.  *
  45.  * Copyright (C) 1984, Sun Microsystems, Inc.
  46.  *
  47.  * These are the "floating point" xdr routines used to (de)serialize
  48.  * most common data items.  See xdr.h for more info on the interface to
  49.  * xdr.
  50.  */
  51.  
  52. #include <sys/param.h>
  53. #include <stdio.h>
  54. #include <rpc/types.h>
  55. #include <rpc/xdr.h>
  56.  
  57. /*
  58.  * NB: Not portable.
  59.  * This routine works on Suns (Sky / 68000's) and Vaxen.
  60.  */
  61.  
  62. #ifdef vax
  63.  
  64. /* What IEEE single precision floating point looks like on a Vax */
  65. struct    ieee_single {
  66.     unsigned int    mantissa: 23;
  67.     unsigned int    exp     : 8;
  68.     unsigned int    sign    : 1;
  69. };
  70.  
  71. /* Vax single precision floating point */
  72. struct    vax_single {
  73.     unsigned int    mantissa1 : 7;
  74.     unsigned int    exp       : 8;
  75.     unsigned int    sign      : 1;
  76.     unsigned int    mantissa2 : 16;
  77. };
  78.  
  79. #define VAX_SNG_BIAS    0x81
  80. #define IEEE_SNG_BIAS    0x7f
  81.  
  82. static struct sgl_limits {
  83.     struct vax_single s;
  84.     struct ieee_single ieee;
  85. } sgl_limits[2] = {
  86.     {{ 0x7f, 0xff, 0x0, 0xffff },    /* Max Vax */
  87.     { 0x0, 0xff, 0x0 }},        /* Max IEEE */
  88.     {{ 0x0, 0x0, 0x0, 0x0 },    /* Min Vax */
  89.     { 0x0, 0x0, 0x0 }}        /* Min IEEE */
  90. };
  91. #endif /* vax */
  92.  
  93. bool_t XDRFUN
  94. xdr_float(xdrs, fp)
  95.     register XDR *xdrs;
  96.     register float *fp;
  97. {
  98. #if !defined(_M68000) && !defined(mc68000) && !defined(sparc)
  99.     struct ieee_single is;
  100.     struct vax_single vs, *vsp;
  101.     struct sgl_limits *lim;
  102.     int i;
  103. #endif
  104.     switch (xdrs->x_op) {
  105.  
  106.     case XDR_ENCODE:
  107. #if defined(_M68000) || defined(mc68000) || defined(sparc)
  108.         return (XDR_PUTLONG(xdrs, (long *)fp));
  109. #else
  110.         vs = *((struct vax_single *)fp);
  111.         for (i = 0, lim = sgl_limits;
  112.             i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  113.             i++, lim++) {
  114.             if ((vs.mantissa2 == lim->s.mantissa2) &&
  115.                 (vs.exp == lim->s.exp) &&
  116.                 (vs.mantissa1 == lim->s.mantissa1)) {
  117.                 is = lim->ieee;
  118.                 goto shipit;
  119.             }
  120.         }
  121.         is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  122.         is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  123.     shipit:
  124.         is.sign = vs.sign;
  125.         return (XDR_PUTLONG(xdrs, (long *)&is));
  126. #endif
  127.  
  128.     case XDR_DECODE:
  129. #if defined(_M68000) || defined(mc68000) || defined(sparc)
  130.         return (XDR_GETLONG(xdrs, (long *)fp));
  131. #else
  132.         vsp = (struct vax_single *)fp;
  133.         if (!XDR_GETLONG(xdrs, (long *)&is))
  134.             return (FALSE);
  135.         for (i = 0, lim = sgl_limits;
  136.             i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  137.             i++, lim++) {
  138.             if ((is.exp == lim->ieee.exp) &&
  139.                 (is.mantissa == lim->ieee.mantissa)) {
  140.                 *vsp = lim->s;
  141.                 goto doneit;
  142.             }
  143.         }
  144.         vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  145.         vsp->mantissa2 = is.mantissa;
  146.         vsp->mantissa1 = (is.mantissa >> 16);
  147.     doneit:
  148.         vsp->sign = is.sign;
  149.         return (TRUE);
  150. #endif
  151.  
  152.     case XDR_FREE:
  153.         return (TRUE);
  154.     }
  155.     return (FALSE);
  156. }
  157.  
  158. /*
  159.  * This routine works on Suns (Sky / 68000's) and Vaxen.
  160.  */
  161.  
  162. #ifdef vax
  163. /* What IEEE double precision floating point looks like on a Vax */
  164. struct    ieee_double {
  165.     unsigned int    mantissa1 : 20;
  166.     unsigned int    exp       : 11;
  167.     unsigned int    sign      : 1;
  168.     unsigned int    mantissa2 : 32;
  169. };
  170.  
  171. /* Vax double precision floating point */
  172. struct  vax_double {
  173.     unsigned int    mantissa1 : 7;
  174.     unsigned int    exp       : 8;
  175.     unsigned int    sign      : 1;
  176.     unsigned int    mantissa2 : 16;
  177.     unsigned int    mantissa3 : 16;
  178.     unsigned int    mantissa4 : 16;
  179. };
  180.  
  181. #define VAX_DBL_BIAS    0x81
  182. #define IEEE_DBL_BIAS    0x3ff
  183. #define MASK(nbits)    ((1 << nbits) - 1)
  184.  
  185. static struct dbl_limits {
  186.     struct    vax_double d;
  187.     struct    ieee_double ieee;
  188. } dbl_limits[2] = {
  189.     {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },    /* Max Vax */
  190.     { 0x0, 0x7ff, 0x0, 0x0 }},            /* Max IEEE */
  191.     {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},        /* Min Vax */
  192.     { 0x0, 0x0, 0x0, 0x0 }}                /* Min IEEE */
  193. };
  194.  
  195. #endif /* vax */
  196.  
  197.  
  198. bool_t XDRFUN
  199. xdr_double(xdrs, dp)
  200.     register XDR *xdrs;
  201.     double *dp;
  202. {
  203.     register long *lp;
  204. #if !defined(_M68000) && !defined(mc68000) && !defined(sparc)
  205.     struct    ieee_double id;
  206.     struct    vax_double vd;
  207.     register struct dbl_limits *lim;
  208.     int i;
  209. #endif
  210.  
  211.     switch (xdrs->x_op) {
  212.  
  213.     case XDR_ENCODE:
  214. #if defined(_M68000) || defined(mc68000) || defined(sparc)
  215.         lp = (long *)dp;
  216. #else
  217.         vd = *((struct vax_double *)dp);
  218.         for (i = 0, lim = dbl_limits;
  219.             i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  220.             i++, lim++) {
  221.             if ((vd.mantissa4 == lim->d.mantissa4) &&
  222.                 (vd.mantissa3 == lim->d.mantissa3) &&
  223.                 (vd.mantissa2 == lim->d.mantissa2) &&
  224.                 (vd.mantissa1 == lim->d.mantissa1) &&
  225.                 (vd.exp == lim->d.exp)) {
  226.                 id = lim->ieee;
  227.                 goto shipit;
  228.             }
  229.         }
  230.         id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  231.         id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  232.         id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  233.                 (vd.mantissa3 << 13) |
  234.                 ((vd.mantissa4 >> 3) & MASK(13));
  235.     shipit:
  236.         id.sign = vd.sign;
  237.         lp = (long *)&id;
  238. #endif
  239.         return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
  240.  
  241.     case XDR_DECODE:
  242. #if defined(_M68000) || defined(mc68000) || defined(sparc)
  243.         lp = (long *)dp;
  244.         return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
  245. #else
  246.         lp = (long *)&id;
  247.         if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  248.             return (FALSE);
  249.         for (i = 0, lim = dbl_limits;
  250.             i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  251.             i++, lim++) {
  252.             if ((id.mantissa2 == lim->ieee.mantissa2) &&
  253.                 (id.mantissa1 == lim->ieee.mantissa1) &&
  254.                 (id.exp == lim->ieee.exp)) {
  255.                 vd = lim->d;
  256.                 goto doneit;
  257.             }
  258.         }
  259.         vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  260.         vd.mantissa1 = (id.mantissa1 >> 13);
  261.         vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  262.                 (id.mantissa2 >> 29);
  263.         vd.mantissa3 = (id.mantissa2 >> 13);
  264.         vd.mantissa4 = (id.mantissa2 << 3);
  265.     doneit:
  266.         vd.sign = id.sign;
  267.         *dp = *((double *)&vd);
  268.         return (TRUE);
  269. #endif
  270.  
  271.     case XDR_FREE:
  272.         return (TRUE);
  273.     }
  274.     return (FALSE);
  275. }
  276.